package com.example.jfinalpluginactiverecord.jfinal_orm;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 集成 JFinal 的 ActiveRecordPlugin 实现轻量级db操作
 */

@Configuration
//@Component
class ActiveRecordPluginConfig {
    @Autowired
    DataSourceConfig dataSourceConfig;


    @Bean
    public ActiveRecordPlugin ininitActiveRecordPlugin() {
        DruidPlugin druidPlugin = new DruidPlugin(dataSourceConfig.getUrl(), dataSourceConfig.getUsername(), dataSourceConfig.getPassword());
        //  DruidPlugin druidPlugin = new DruidPlugin("jdbc:mysql://192.168.37.129:3306/jfinaltest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true", "root", "OG123nuttertoolstf@@#=+");
        // 加强数据库安全
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        // 添加 StatFilter 才会有统计数据
        druidPlugin.addFilter(new StatFilter());
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(true);
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        // sql文件路径在src/mian/resources/sql/all_sqls.sql  新建这个文件
        arp.addSqlTemplate("/jfinal_orm/sql/all_sqls.sql");
        //	arp.addMapping("blog", Blog.class);
        // 与 jfinal web 环境唯一的不同是要手动调用一次相关插件的start()方法
        druidPlugin.start();
        arp.start();
        return arp;
    }
}







